package edu.hunau.cxb.controller;

import edu.hunau.cxb.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import java.util.Map;
import java.util.Objects;

/**
 * @author hb
 * @create 2020-04-07 10:56
 */
@RequestMapping("/user")
@Controller
//session的key为users,数据类型为User
@SessionAttributes(value = "users", types = {User.class})
public class UserManagerController {
/*
     @ModelAttribute("users")
    public Object getModel(User user){
        return user;
    }*/

    //@RequestMapping(value="/login" ,method = RequestMethod.POST)
    @PostMapping("/login")
    public String login(User user, Map<String, Object> map) {
        System.out.println(user);
        if (!Objects.isNull(user) && "250".equals(user.getPwd())) {
            System.out.println("登录成功");
            map.put("users",user);
            return "redirect:lists.html";
        }
        map.put("errorMessage", "用户名或者密码错误!");

        return "login";
    }

    @GetMapping("/logout")
    public String logOut(SessionStatus status){
        System.out.println("清除session中所有的数据");
        //清除session中的数据
        status.setComplete();
        //redirect后面的请求中开头的斜杠 表示从根路径开始
        return "redirect:/login.html";
    }
}
